﻿using System;
using System.Collections.Generic;
using System.Web.Mvc;

namespace EasyUIMvc.Tree
{
    public class TreeExtension : ExtensionBase
    {
        private readonly TreeSettings _settings;
        public TreeExtension(TreeSettings settings)
        {
            _settings = settings;
        }
        private List<TreeNode> _nodes;

        public TreeExtension Bind(List<TreeNode> nodes)
        {
            _nodes = nodes;
            return this;
        }

        //private IHierarchicalEnumerable _hierarchical;
        //private Action<object, TagBuilder> _setNodeAction;
        //private Func<object, string> _getNodeTextFunc;
        ///// <summary>
        ///// Bind to IHierarchicalEnumerable
        ///// </summary>
        ///// <param name="hierarchical">IHierarchicalEnumerable object</param>
        ///// <param name="getNodeText">Func for set node text, p1(data item), return(span tag)</param>
        ///// <param name="setNode">Action for set node other properties, p1(data item), p2(li tag)</param>
        ///// <returns></returns>
        //public TreeExtension Bind(IHierarchicalEnumerable hierarchical,
        //    Func<object, string> getNodeText = null, Action<object, TagBuilder> setNode = null)
        //{
        //    _getNodeTextFunc = getNodeText;
        //    _setNodeAction = setNode;
        //    _hierarchical = hierarchical;
        //    return this;
        //}

        public override string GetScripts()
        {
            throw new NotImplementedException();
        }
        public override MvcHtmlString GetHtml()
        {
            if (!string.IsNullOrEmpty(_settings.LoadFilterFunction))
            {
                _settings.DataOptions += "," + _settings.LoadFilterFunction;
            }
            var tree = GetTopTag("ul", "easyui-tree", _settings);
            return MvcHtmlString.Create(tree.ToString());
        }
        //public override MvcHtmlString GetHtml()
        //{
        //    var top = GetTopTag("ul", "easyui-tree", _settings);

        //    if (_hierarchical != null)
        //    {
        //        AddTreeNode(top, _hierarchical);
        //    }

        //    return MvcHtmlString.Create(top.ToString());
        //}

        //private void AddTreeNode(TagBuilder parent, IHierarchicalEnumerable hierarchical)
        //{
        //    foreach (var item in hierarchical)
        //    {
        //        var li = new TagBuilder("li");
        //        var span = new TagBuilder("span");
        //        var data = _hierarchical.GetHierarchyData(item);
        //        var dataItem = data.Item;
        //        span.SetInnerText(_getNodeTextFunc == null ?
        //            dataItem.ToString() : _getNodeTextFunc(dataItem));
        //        li.InnerHtml += span.ToString();
        //        if (_setNodeAction != null)
        //            _setNodeAction(item, li);
        //        if (data.HasChildren)
        //        {
        //            var children = data.GetChildren();
        //            var childTag = new TagBuilder("ul");
        //            AddTreeNode(childTag, children);
        //            li.InnerHtml += childTag.ToString();
        //        }
        //        parent.InnerHtml += li.ToString();
        //    }
        //}
    }

    public class TreeSettings : SettingsBase
    {
        public string LoadFilterFunction { get; set; }
    }
}